﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Runda.CMS.Dal
{
    public partial class CtrtContractItemPartDal
    {
        public DataTable GetContractItemPart(Guid ItemID)
        {
            string sql = @"select A.PartID,A.ItemID,A.Count,B.* from Cms_Ctrt_ContractItemPart A join Cms_Ctrt_Instrument B on A.InstrumentID = B.InstrumentGUID where A.ItemID = @ItemID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@ItemID", DbType.Guid, ItemID);
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public decimal GetContractItemPartCost(Guid ContractID)
        {
            string sql = @"
select isnull(sum(A.Count * B.GuaranteeYear * C.Cost),0) from Cms_Ctrt_ContractItemPart A 
JOIN Cms_Ctrt_ContractItem B on A.ItemID = B.ItemID 
join Cms_Ctrt_Instrument C on A.InstrumentID = C.InstrumentGUID where B.ContractID = @ContractID
";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@ContractID", DbType.Guid, ContractID);
            return (decimal)CurrentDatabase.ExecuteScalar(dbcommand);
        } 
    }
}
